package com.errorfinding.common;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class test2 {

    /**
     * 目录json
     */
    public static void tree(File file, int index, int parentIndex, String parentFix) {
        String nowFix = "";
        if (parentIndex == 0) {
            nowFix = parentFix + "  ";
        } else if(parentIndex > 0) {
            nowFix = parentFix + "| ";
        }

        if (file.isDirectory()) {
            // 文件夹目录树[只包含目录]
            System.out.println(nowFix + (parentIndex >= 0 ? "|-" : "") + file.getName());

            File[] files = file.listFiles(item -> !item.isHidden());
            if (files == null) return;
            List<File> fileList = Arrays.stream(files).sorted((f1, f2) -> f2.getName().compareToIgnoreCase(f1.getName())).collect(Collectors.toList());
            for (int i = fileList.size() - 1; i >= 0; --i) {
                tree(fileList.get(i), i, index, nowFix);
            }
        }
    }

    public static void main(String[] args) {
        File file = new File("C:\\java\\errorfinding-java\\src\\main\\java\\com\\errorfinding");
        // 文件目录树
        tree(file, 0, -1, "");
    }
}
